from django.db import models

# Things needed from NoPBX
from nopbx.core.models import BaseModule


###############################################################################

class DayManager(models.Manager):
    def get_by_natural_key(self, name):
        return self.get(name = name);

class Day(BaseModule):
    """
    Daily Schedule
      This represents time of day scheduling per extension.
    """
    name        = models.CharField(max_length = 50, unique = True)

    start       = models.TimeField(default = '08:00:00')
    end         = models.TimeField(default = '17:00:00')

    objects = DayManager()
    class Meta:
        ordering = ['name', 'start', 'end']

    # Functions
    def __unicode__(self):
        return self.name 

    def natural_key(self):
        return self.name



###############################################################################

class WeekManager(models.Manager):
    def get_by_natural_key(self, name):
        return self.get(name = name)

class Week(BaseModule):
    """
    Week Schedules
      Collection of Days
    """
    name = models.CharField(max_length = 50, unique = True)

    # Days
    mon = models.ForeignKey(Day, related_name='mon', blank = True, null = True)
    tue = models.ForeignKey(Day, related_name='tue', blank = True, null = True)
    wed = models.ForeignKey(Day, related_name='wed', blank = True, null = True)
    thu = models.ForeignKey(Day, related_name='thu', blank = True, null = True)
    fri = models.ForeignKey(Day, related_name='fri', blank = True, null = True)
    sat = models.ForeignKey(Day, related_name='sat', blank = True, null = True)
    sun = models.ForeignKey(Day, related_name='sun', blank = True, null = True)

    objects = WeekManager()
    class Meta:
        ordering = ['name']

    # Functions
    def __unicode__(self):
        return self.name

    def natural_key(self):
        return self.name



###############################################################################

class HolidayManager(models.Manager):
    def get_by_natural_key(self, name):
        return self.get(name = name)

class Holiday(BaseModule):
    """
    Closed days
      This represents days that the extension is closed.
    """
    name  = models.CharField(max_length = 50, unique = True)

    start = models.DateField()
    end   = models.DateField(blank = True, null = True)

    objects = HolidayManager()
    class Meta:
        ordering = ['start', 'end', 'name']

    # Functions
    def __unicode__(self):
        return self.name

    def natural_key(self):
        return self.name



###############################################################################

class CalendarManager(models.Manager):
    def get_by_natural_key(self, name):
        return self.get(name = name)

class Calendar(BaseModule):
    """
    """
    name     = models.CharField(max_length = 50, unique = True)
    holidays = models.ManyToManyField(Holiday, blank = True, null = True)
    week     = models.ForeignKey(Week, blank = True, null = True)

    objects = CalendarManager()
    class Meta:
        ordering = ['name']

    def __unicode__(self):
        return self.name

    def natural_key(self):
        return self.name



